package com.ftg.learn.chapter09.listtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 列表元素交替合并
 * 编写一个函数，接受两个整数列表，并将它们的元素交替合并到一个新的列表中。
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/16 15:40
 */

public class merge {

    public static void main(String[] args) {

        List<Integer> list1=new ArrayList<>();
        System.out.println("输入链表1中数的个数");
        Scanner scanner=new Scanner(System.in);
        int count1=scanner.nextInt();
        int num1;
        System.out.println("输入数字");
        for (int i = 0; i < count1; i++) {
            num1= scanner.nextInt();
            list1.add(num1);
        }
        System.out.println(list1);

        List<Integer> list2=new ArrayList<>();
        System.out.println("输入链表2中数的个数");
        int count2=scanner.nextInt();
        int num2;
        System.out.println("输入数字");
        for (int i = 0; i < count2; i++) {
            num2= scanner.nextInt();
            list2.add(num2);
        }
        System.out.println(list2);

        List<Integer> list=new ArrayList<>();
        int count3= Math.max(count1, count2);
        int count= Math.min(count1, count2);
        for (int i = 0,j=0; i <count3; i++,j++) {
            if (i<count||i>count){
                list.add(list1.get(i));
            }
            if(j<count||j>count){
                list.add(list2.get(j));
            }
        }
        System.out.println(list);
    }
}
